package com.superpai.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.superpai.R;
import com.superpai.cache.ActivityManager;
import com.superpai.cache.CacheCategory;
import com.superpai.view.db.CartDBHelper;
import com.superpai.view.helper.BaseTask;
import com.superpai.view.helper.IDataConListener;

/**
 * @author Devin.Hu
 * @date 2011-8-14
 * @version V1.0
 * @description 继承页面
 */
public abstract class BaseActivity extends Activity implements OnClickListener, IDataConListener {

	private BaseTask mBaseTask;
	protected TextView title;
	protected TextView productCartnum;
	protected Context mContext;
	protected ProgressDialog mDialog;
	protected ProgressBar mProgress;
	protected TextView mNodata;
	protected Button mRefresh;

	protected View mMenuHome;
	protected View mMenuBrand;
	protected View mMenuSCart;
	protected View mMenuPaiPai;
	protected View mMenuMore;

	protected int mIndexHome = 1;
	protected int mIndexBrand = 2;
	protected int mIndexCart = 3;
	protected int mIndexPaiPai = 4;
	protected int mIndexMore = 5;

	protected final int STATUS_SUC = 1;
	protected final int STATUS_ERR = -1;
	protected final int STATUS_NODATA = 0;
	protected Activity curActivity;
	private ActivityManager activityManager = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		activityManager = ActivityManager.getScreenManager();
		activityManager.pushActivity(this);

	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		activityManager.popActivity(this);
	}

	/**
	 * 初始化菜单模块
	 * 
	 * @param index
	 */
	protected void initMenuView(Activity activity, int index) {
		
		curActivity = activity;

		mMenuHome = findViewById(R.id.menu_home);
		mMenuBrand = findViewById(R.id.menu_brand);
		mMenuSCart = findViewById(R.id.menu_cart);
		mMenuPaiPai = findViewById(R.id.menu_paipai);
		mMenuMore = findViewById(R.id.menu_more);
		productCartnum = (TextView) this.findViewById(R.id.productcartnum);

		this.mMenuHome.setOnClickListener(this);
		this.mMenuBrand.setOnClickListener(this);
		this.mMenuSCart.setOnClickListener(this);
		this.mMenuPaiPai.setOnClickListener(this);
		this.mMenuMore.setOnClickListener(this);

		if (index == mIndexHome)
			mMenuHome.setBackgroundResource(R.drawable.menu_pressed);
		if (index == mIndexBrand)
			mMenuBrand.setBackgroundResource(R.drawable.menu_pressed);
		if (index == mIndexCart)
			mMenuSCart.setBackgroundResource(R.drawable.menu_pressed);
		if (index == mIndexPaiPai)
			mMenuPaiPai.setBackgroundResource(R.drawable.menu_pressed);
		if (index == mIndexMore)
			mMenuMore.setBackgroundResource(R.drawable.menu_pressed);

		setProductCartNum(curActivity);
	}

	/**
	 * 让底图check到当前的页面
	 * 
	 * @param paramView
	 */
	public void checkMenu(View paramView) {

		Intent intent = new Intent();

		if (paramView == mMenuHome) {
			intent.setClass(this, HomeActivity.class);
			startActivity(intent);
		}

		if (paramView == mMenuBrand) {
			intent.setClass(this, CategoryActivity.class);
			startActivity(intent);
		}

		if (paramView == mMenuSCart) {
			intent.setClass(this, CartActivity.class);
			startActivity(intent);
		}

		if (paramView == mMenuPaiPai) {
			intent.setClass(this, MyPaipaiActivity.class);
			startActivity(intent);
		}

		if (paramView == mMenuMore) {
			intent.setClass(this, MoreActivity.class);
			startActivity(intent);
		}
	}


	/**
	 * 设置购物袋数量
	 * 
	 * @param num
	 */
	public void setProductCartNum(Context mContext) {
		CartDBHelper db = new CartDBHelper(mContext);
		long num = db.getCount();
		if (num >= 0) {
			productCartnum.setVisibility(View.VISIBLE);
			productCartnum.setText(String.valueOf(num));
		} else {
			productCartnum.setVisibility(View.GONE);
		}
	}

	/**
	 * 开启连接
	 * 
	 * @param connectionType
	 */
	public void onConnection(int connectionType) {
		mBaseTask = new BaseTask();
		mBaseTask.setmDataListener(this);
		mBaseTask.connection(connectionType);
	}

	/**
	 * 是否正在连接
	 * 
	 * @return
	 */
	public boolean isConnection() {
		if (null != mBaseTask) {
			return mBaseTask.isConnection();
		}
		return false;
	}

	/**
	 * 关闭连接
	 */
	public void disConnection() {
		if (null != mBaseTask)
			mBaseTask.disConnection();
	}

	/**
	 * 退出系统
	 */
	public void sysExit() {
		disConnection();
		//CacheCategory.clearCacheCategory();
		activityManager.popAllActivityExceptOne(null);
	}
}
